package com.googlecode.jlogindexer.client;

import java.io.File;
import java.util.List;

import noNamespace.AlertDocument.Alert;
import noNamespace.LogsDocument.Logs;

import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListener;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.googlecode.jlogindexer.client.exception.MappingException;
import com.googlecode.jlogindexer.client.utils.JLogalizerAlertConfiguration;
import com.googlecode.jlogindexer.client.utils.JLogalizerConfigBuilder;

public class JLogalizerClient  implements ApplicationContextAware{
	
	//BeanFactory context=SpringUtils.getContext();
	private ApplicationContext context=null;
	private JLogalizerConfigBuilder alertConfigBuilder;
	

	public void startTailing() throws InterruptedException, MappingException{
		
		//FIXME Pseudocode - to be implemented later
		/*1. File configuration to be read from XML.
		 *2. XML to store unique id of the client, hostname and filepath
		 *3. Read each configuration (inside configurations tag) and construct thread   
		 */
		
		/*List<Configuration> configurations = configBuilder.getConfigurations().getConfiguration();
		for (Configuration configuration : configurations) {
			
			startThreads(xxx, configuration);
			
		}*/
		startThreads();
		//this will help the client not to exit
		/*Object obj=new Object();
		synchronized(obj){
			obj.wait();
		}*/
	}

	
	//private void startThreads(xxxx, Configuration configuration) {
		
	private void startThreads() throws MappingException{
		
		JLogalizerAlertConfiguration alertConfiguration = alertConfigBuilder.getAlertConfiguration();
		List<Alert> alerts = alertConfiguration.getAlerts();
		
		if (alerts!=null){
			for (Alert alert: alerts){
				String clientId=alert.getClientId();
				String hostname=alert.getHostname();
				//String filePath="d:/logs/server.log";
				Logs logs=alert.getLogs();
				
				if (logs!=null){
					for (String filePath : logs.getPathArray()) {
						
						String threadName=StringUtils.trimToEmpty(clientId+"_"+hostname+"_"+filePath);
						
						
						TailerListener listener=(JLogalizerTailerListener)context.getBean("jlogalizerTailerListener", clientId, hostname, filePath);
						
						//TailerListener listener = new JLogalizerTailerListener(hostname, filePath);
						Tailer tailer = new Tailer(new File(filePath), listener, 1000, true);
						System.out.println(tailer.getFile().getAbsolutePath());
						System.out.println(tailer.getDelay());
					    Thread thread = new Thread(tailer, threadName);
					    thread.start();
						
						
					}
				}
				
			}
		}
		
		
		
	}


	public JLogalizerConfigBuilder getAlertConfigBuilder() {
		return alertConfigBuilder;
	}


	public void setAlertConfigBuilder(JLogalizerConfigBuilder alertConfigBuilder) {
		this.alertConfigBuilder = alertConfigBuilder;
	}


	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		
		this.context=context;
	}
	
	
	
}


